﻿
CREATE PROCEDURE [dbo].[DefekturaCategoriesVsDepartmentsUpdate]
(
	@DefekturaCategoryId int,
	@DepartmentId int,
	@SeekDuration int,
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50),
	@ToDelete bit,
	@Original_Id int,
	@Original_MD datetime
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @orgDefekturaCategoryId int, @orgDepartmentId int
DECLARE @orgToDelete bit, @orgParentToDelete bit
	SET NOCOUNT ON

IF (@DefekturaCategoryId Is Null) OR (@DepartmentId Is Null) OR (@SeekDuration Is Null) OR (@ToDelete Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @orgDefekturaCategoryId = DefekturaCategoryId, @orgDepartmentId = DepartmentId,
@orgToDelete = ToDelete
FROM CA.DefekturaCategoriesVsDepartments WHERE Id = @Original_Id AND MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 2, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@orgDefekturaCategoryId <> @DefekturaCategoryId) OR (@orgDepartmentId <> @DepartmentId)
BEGIN
	IF EXISTS (SELECT TOP 1 ID FROM CA.DefekturaCategoriesVsDepartments 
	WHERE DefekturaCategoryId = @DefekturaCategoryId 
	AND DepartmentId = @DepartmentId
	AND ToDelete = 0) 
	BEGIN
		SELECT @intErrCode = 4, @strMsg = 'Дублируется категория для подразделения'
		RAISERROR (@strMsg, 11, @intErrCode)
		RETURN @intErrCode
	END
END

SELECT @orgParentToDelete = ToDelete
FROM CO.DefekturaCategories WHERE Id = @DefekturaCategoryId
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 5, 
	@strMsg = 'Отсутствует родительская запись с кодом ' + cast(@DefekturaCategoryId as varchar(20)) + ' в справочнике категорий!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @orgParentToDelete = 1 
BEGIN
	SELECT @intErrCode = 6, 
	@strMsg = 'Родительская запись с кодом ' + cast(@DefekturaCategoryId as varchar(20)) + ' в справочнике категорий отмечена для удаления!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @orgToDelete = 1 --Запись была отмечена для удаления
BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 7, 
		@strMsg = 'Нельзя редактировать запись, отмеченную для удаления!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END ELSE BEGIN
	IF @ToDelete = 1 
	BEGIN
		SELECT @intErrCode = 8, 
		@strMsg = 'Для удаления данных должна использоваться другая процедура!'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END

EXEC [dbo].[ProcessStatistic] @Note OUTPUT, @MD OUTPUT, @MW OUTPUT
	SET NOCOUNT OFF;

UPDATE CA.DefekturaCategoriesVsDepartments SET 
DefekturaCategoryId = @DefekturaCategoryId, 
DepartmentId = @DepartmentId, 
SeekDuration = @SeekDuration,
[Note] = @Note, [MD] = @MD, [MW] = @MW, ToDelete = @ToDelete  
WHERE Id = @Original_Id AND MD = @Original_MD;
	
SELECT Id, DefekturaCategoryId AS CategoryId, DepartmentId, SeekDuration, Note, 
MD AS ChangeDateTime, MW AS ChangeWhom, ToDelete
FROM CA.DefekturaCategoriesVsDepartments WHERE (Id = @Original_Id)

RETURN 0
